package UnionTypes

trait Division

case class DivisionByZero(msg: String) extends Division
case class Success(double: Double) extends Division

type DivisionResult = DivisionByZero | Success

trait List[+A]
case class Cons[A](h: A, t: List[A]) extends List[A]
case object Empty extends List[Nothing]

def safeDivide(a: Double, b: Double): DivisionResult =
  if b == 0 then
    DivisionByZero("DivisionByZeroException")
  else
    Success(a / b)

def either(division: Division) = division match
  case DivisionByZero(msg) => Left(msg)
  case Success(d) => Right(d)


def test: Unit =
  val divisionResultSuccess: DivisionResult = safeDivide(4, 2)
  val divisionResultFailure: Success | DivisionByZero = safeDivide(4, 0)

  println(either(divisionResultSuccess))
  println(either(divisionResultFailure))

  val list: Cons[Int] | Empty.type = Cons(1, Cons(2, Cons(3, Empty)))
  val emptyList: Empty.type | Cons[Any] = Empty
  println(list)
  println(emptyList)